package com.donnie.shopping.config;

import com.donnie.shopping.enums.Fruit;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * @description: 水果配置初始化
 * @author: ChenDeXin
 * @time: 2025/5/20 11:19
 */
@Configuration
public class FruitConfInitializer implements ApplicationRunner {
    @Resource
    FruitConfig fruitConfig;

    @Override
    public void run(ApplicationArguments args) {
        Set<Map.Entry<String, Double>> priceEntries = fruitConfig.getPriceList().entrySet();

        for (Map.Entry<String, Double> entry : priceEntries) {
            try {
                Fruit fruit = Fruit.valueOf(entry.getKey());
                fruit.setPrice(entry.getValue());
            } catch (IllegalArgumentException ignored) {
                // 忽略未定义的水果
            }
        }
    }
}
